﻿using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using Model;

namespace Control
{
    public class MGTemplates : ModuloDeGerenciamento<Template>
    {

        public MGTemplates()
        {
            this.dc = new DataClassesDataContext();
        }

        public override List<Template> listar()
        {
            var templates = from row in this.dc.Templates select row;
            List<Template> list = new List<Template>();
            foreach (Template t in templates)
            {
                list.Add(t);
            }
            return list;
        }

        public override bool adicionar(Template obj)
        {
            bool flag = true;
            try
            {
                this.dc.Templates.InsertOnSubmit(obj);
                this.dc.SubmitChanges();
            }
            catch (Exception e)
            {
                this.output += "Erro ao inserir o registro";
                this.output += e.ToString();
                flag = false;
            }
            return flag;
        }

        public bool adicionar(String nome, String arquivo)
        {
            if (String.IsNullOrEmpty(nome) || String.IsNullOrEmpty(arquivo))
                return false;
            Template t = new Template();
            t.nome = nome;
            t.arquivo = arquivo;
            return this.adicionar(t);
        }

        public override bool remover(Template obj)
        {
            bool flag = true;
            try
            {
                this.dc.Templates.DeleteOnSubmit(obj);
                this.dc.SubmitChanges();
            }
            catch (Exception e)
            {
                this.output += "Erro ao remover o registro";
                this.output += e.ToString();
                flag = false;
            }
            return flag;
        }

        public override Template get(uint id)
        {
            if (id == 0) return null;
            Template template = null;
            try
            {
                template = this.dc.Templates.Single(t => t.id == id);
            }
            catch (Exception e)
            {
                this.output += "Erro ao obter o registro";
                this.output += e.ToString();
                template = null;
            }
            return template;
        }

        public override bool remover(uint id)
        {
            return this.remover(this.get(id));
        }

    }
}
